package com.gw.quotes.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.gw.quotes.MyApplication;
import com.gw.quotes.R;
import com.gw.quotes.activity.QuoteDetailActivity;
import com.gw.quotes.entity.business.ProjectInfoEntity.ProjectInfo;
import com.gw.quotes.entity.business.ProjectListEntity.ProjectItem;
import com.gw.quotes.entity.system.User;
import com.gw.quotes.net.BaseServiceInter;
import com.gw.quotes.net.HttpCallBack;
import com.gw.quotes.net.Request;
import com.gw.quotes.net.Response;
import com.gw.quotes.net.service.ProjectInfoQueryServiceImpl;
import com.gw.quotes.util.CommonTools;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.yxw.view.YXWProgressDialog;
import com.yxw.view.YXWProgressDialog.YXWDialog;

public class QuoteAdapter extends BaseAdapter {

	private ArrayList<ProjectItem> list;
	private Context context;
	private int resource;
	protected ImageLoader imageLoader;
	protected DisplayImageOptions options;
	private ProjectItem projectItem;
	protected YXWDialog dialog;

	public QuoteAdapter(Context context, int layoutId,
			ArrayList<ProjectItem> list) {
		this.context = context;
		this.resource = layoutId;

		// 初始化dialog
		dialog = new YXWProgressDialog(context).createDialog("加载中…");

		this.list = list;
		this.imageLoader = ImageLoader.getInstance();
		this.imageLoader.init(ImageLoaderConfiguration.createDefault(context));
		this.options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_empty)
				.showImageForEmptyUri(R.drawable.ic_empty)
				.showImageOnFail(R.drawable.ic_empty).cacheInMemory(true)
				.cacheOnDisk(true).displayer(new FadeInBitmapDisplayer(500))// 是否图片加载好后渐入的动画时间
				.bitmapConfig(Bitmap.Config.RGB_565).build();
	}

	public void clear() {
		if (list != null) {
			list.clear();
			notifyDataSetChanged();
		}
	}

	public ArrayList<ProjectItem> getListData() {
		return list;
	}

	public void setListData(ArrayList<ProjectItem> list) {
		this.list = list;
	}

	@Override
	public int getCount() {
		return this.list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		projectItem = list.get(position);

		if (convertView == null) {
			convertView = (LinearLayout) LayoutInflater.from(this.context)
					.inflate(resource, null);
		}
		// 默认为0 还没请求数据
		if (projectItem.getHavequote() == 0) {
			requestInfo(projectItem, convertView);
			convertView.findViewById(R.id.progressbar).setVisibility(
					View.VISIBLE);
			convertView.findViewById(R.id.quote).setVisibility(View.GONE);
			convertView.findViewById(R.id.quote_btn).setVisibility(View.GONE);
		} else if (projectItem.getHavequote() == 1) {
			convertView.findViewById(R.id.progressbar).setVisibility(View.GONE);
			convertView.findViewById(R.id.quote).setVisibility(View.VISIBLE);
			convertView.findViewById(R.id.quote_btn).setVisibility(View.GONE);

		} else {
			convertView.findViewById(R.id.progressbar).setVisibility(View.GONE);
			convertView.findViewById(R.id.quote).setVisibility(View.GONE);
			convertView.findViewById(R.id.quote_btn)
					.setVisibility(View.VISIBLE);
		}

		ImageView quote_img = (ImageView) convertView
				.findViewById(R.id.quote_img);
		TextView goods_title = (TextView) convertView
				.findViewById(R.id.goods_title);
		TextView project_code = (TextView) convertView
		.findViewById(R.id.project_code_text);
		TextView goods_name = (TextView) convertView
				.findViewById(R.id.goods_name_text);
		TextView merchants_name = (TextView) convertView
				.findViewById(R.id.merchants_name_text);
		TextView join_supplier = (TextView) convertView
				.findViewById(R.id.join_supplier_text);
		TextView quote_number = (TextView) convertView
				.findViewById(R.id.quote_number_text);
		TextView end_time = (TextView) convertView
				.findViewById(R.id.end_time_text);
		// 因为我这边没什么需求，到时候再根据需求set相应的image
		// quote_img.setImageResource(resId);
		if (projectItem.getProjectGoodList() != null
				&& projectItem.getProjectGoodList().size() > 0) {
			imageLoader.displayImage(projectItem.getProjectGoodList().get(0)
					.getImgurl(), quote_img, options);
		} else {
			quote_img.setImageResource(R.drawable.ic_empty);
		}
		goods_title.setText(PROJCETTYPE[projectItem.getProjecttype() - 1]);
		// System.out.println(position+":"+projectItem.getProjecttype());
		project_code.setText(projectItem.getProjectcode());
		goods_name.setText(projectItem.getGoodsclassname());
		if (projectItem.getProjectGoodList() != null
				&& projectItem.getProjectGoodList().size() > 0
				&& projectItem.getProjectGoodList().get(0) != null) {
			goods_name.setText(projectItem.getProjectGoodList().get(0)
					.getGoodsname());
		}
		merchants_name.setText(projectItem.getBuyerorgname());
		join_supplier.setText(projectItem.getQuotationcount() + "");
		quote_number.setText(projectItem.getQty() + "");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		end_time.setText(df.format(projectItem.getEndtime()));
		return convertView;
	}

	private void requestInfo(final ProjectItem projectItem, final View view) {
		//dialog.show();
		BaseServiceInter httpService = new ProjectInfoQueryServiceImpl();
		Request request = new Request();
		request.setProjectguid(projectItem.getProjectguid());
		request.setProjecttype(projectItem.getProjecttype());
		request.setLoginname(getLoginUser().getLoginname());
		request.setTicket(getLoginUser().getTicket());
		Response response = httpService.handler(request,
				new HttpCallBack<Response<ProjectInfo>>() {
					@Override
					public void onFinished(Response<ProjectInfo> response) {
						if (response != null) {
							if (response.getResult() == response.RESULT_SUCCESS) {
								ProjectInfo projectInfo = response.getObj();
								if (projectInfo.getProjectquotationList()!=null&&projectInfo.getProjectquotationList()
										.size() > 0) {

									// 已经报价,随机测试
									// if ((int) (Math.random() * 2) == 0) {
									// 1。表示已报价
									projectItem.setHavequote(1);
									// 屏蔽"报价"按钮,显示-已报价
									view.findViewById(R.id.progressbar).setVisibility(View.GONE);
									view.findViewById(R.id.quote).setVisibility(View.VISIBLE);
									view.findViewById(R.id.quote_btn).setVisibility(View.GONE);
		
									// 屏蔽"报价"事件
									// view.findViewById(R.id.button_layout).setOnClickListener(null);
									view.findViewById(R.id.button_layout)
										.setOnClickListener(
												new OnClickListener() {

													@Override
													public void onClick(
															View v) {
														Intent intent = new Intent(
																context,
																QuoteDetailActivity.class);
														intent.putExtra(
																"projectItem",
																projectItem);
														context.startActivity(intent);

													}
												});
									// } else {
									// }
								} else {
									// -1。表示未报价
									projectItem.setHavequote(-1);
									// 屏蔽"已报价"按钮,显示-报价
									view.findViewById(R.id.progressbar)
											.setVisibility(View.GONE);
									view.findViewById(R.id.quote)
											.setVisibility(View.GONE);
									view.findViewById(R.id.quote_btn)
											.setVisibility(View.VISIBLE);
									view.findViewById(R.id.button_layout)
											.setOnClickListener(
													new OnClickListener() {

														@Override
														public void onClick(
																View v) {
															Intent intent = new Intent(
																	context,
																	QuoteDetailActivity.class);
															intent.putExtra(
																	"projectItem",
																	projectItem);
															context.startActivity(intent);

														}
													});
//									CommonTools.showShortToast(context,
//											"一直在转圈的是报价供应商为0！");
								}
							} else {
								CommonTools.showShortToast(context,
										response.getMsg());
							}
						} else {
							CommonTools.showShortToast(context, "信息获取失败！");
						}
						// dialog.dismiss();
					}
				});
	}

	protected User getLoginUser() {
		return MyApplication.instance().getLoginUser();
	}

	public static final String[] PROJCETTYPE = { "竞价", "议价", "单品牌批量采购",
			"多品牌批量采购" };

}
